package org.dsbackend.controller;


import org.dsbackend.entity.JsonData;
import org.dsbackend.entity.ResultException;
import org.dsbackend.model.Disease;
import org.dsbackend.repository.DiseaseRepository;
import org.dsbackend.util.CharUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

@RestController
@RequestMapping(value="/diseases")
public class DiseaseController {
    @Autowired
    private DiseaseRepository diseaseRepository;

    @GetMapping(value="/{id}")
    public JsonData getDisease (@PathVariable Long id) {
        Disease disease;
        try{
            disease = diseaseRepository.findById(id).get();
        }
        catch (NoSuchElementException e) {
            throw new ResultException(401, "No disease found");
        }
        return new JsonData(0, "success", disease);
    }

    @GetMapping(value="/")
    public JsonData getDiseaseList() {
        List<Disease> diseases;
        try {
            diseases = diseaseRepository.findAll();
        }
        catch (Exception e) {
            throw new ResultException(402, e.toString());
        }
        Iterator<Disease> iter = diseases.iterator();
        while (iter.hasNext()) {
            Disease temp = iter.next();
            if (!temp.getDiseaseAvailable()) iter.remove();
        }
        if (diseases.isEmpty())
            throw new ResultException(401, "No disease found");
        return new JsonData(0, "success", diseases);
    }
    @PostMapping(value="/")
    public JsonData postDisease(@RequestParam(name="diseaseName") String diseaseName) {
        if (diseaseName.equals("") || CharUtils.hasIllegalChar((diseaseName))) {
            throw new ResultException(403, "diseaseName is empty or contains illegal character");
        }
        else {
            Disease disease = new Disease(diseaseName);
            try {
                diseaseRepository.save(disease);
            }
            catch (DataIntegrityViolationException e) {
                throw new ResultException(403, "Duplicate diseaseName");
            }
            catch (Exception e) {
                throw new ResultException(402, e.toString());
            }
            return new JsonData(0, "success", null);
        }
    }

    @PutMapping(value="/{id}")
    public JsonData putDisease(@PathVariable Long id,
                               @RequestParam(name="diseaseName", required = false) String diseaseName) {
        Disease disease;
        try {
            disease = diseaseRepository.findById(id).get();
        }
        catch (NoSuchElementException e) {
            throw new ResultException(401, "No disease found");
        }
        if (diseaseName != null && !diseaseName.equals("")) {
            if (CharUtils.hasIllegalChar(diseaseName)) {
                throw new ResultException(403, "diseaseName contains illegal character");
            }
            else {
                disease.setDiseaseName(diseaseName);
            }
        }
        try {
            diseaseRepository.save(disease);
        }
        catch (DataIntegrityViolationException e)
        {
            throw new ResultException(403,"Duplicate diseaseName");
        }
        catch (Exception e) {
            throw new ResultException(402, e.toString());
        }

        return new JsonData(0, "success", null);
    }

    @DeleteMapping(value="/{id}")
    public JsonData deleteDisease (@PathVariable Long id) {
        Disease disease;
        try {
            disease = diseaseRepository.findById(id).get();
        }
        catch (NoSuchElementException e) {
            throw new ResultException(401, "No disease found");
        }
        disease.setDiseaseAvailable(false);
        try {
            diseaseRepository.save(disease);
        }
        catch (Exception e) {
            throw new ResultException(402, e.toString());
        }
        return new JsonData(0,"success",null);
    }

}
